﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kooboo.Commerce;

using Kooboo.IoC;

namespace Kooboo.Web.Areas.Commerce.Binders
{
    public class BrandBinder:EntityBinder
    {
        protected override string[] SkipProperties
        {
            get
            {
                return new string[] { "ProductTypes" };
            }
        }

        protected override object CreateModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext, Type modelType)
        {
            var model = ObjectContainer.CreateInstance<IBrand>();

            var form = controllerContext.HttpContext.Request.Form;
            var productTypes = form["ProductTypeList"];

            if (string.IsNullOrWhiteSpace(productTypes) == false)
            {
                var list = new List<IProductType>();
                var ids = productTypes.Split( new char[]{','},StringSplitOptions.RemoveEmptyEntries);

                foreach (var id in ids)
                {
                    var item = ObjectContainer.CreateInstance<IProductType>();
                    item.Id = int.Parse(id);
                    list.Add(item);
                }

                model.ProductTypes = list.AsQueryable();
            }

            return model;
        }
    }
}